<%@ page contentType="text/html; charset=utf-8"%>

<%@ include file="/WEB-INF/jsp/includeTaglib.jsp" %>

<form id="pageForm">
	<input type="hidden" name="pageDirection" value="">

	<div align="right" class="globalText">
		    <c:if test="${isFirstPage}">上一页</c:if>
		    <c:if test="${!isFirstPage}">
		      <input type="hidden" name="nextPageIndex" value="${previousPageIndex}">
		      <input type="hidden" name="recordStartIndex" value="${previousRecordStartIndex}">
					<a href="javascript:void(0)" id="goPreviousPage.htm">上一页</a>
		    </c:if>
		    &nbsp;&nbsp;&nbsp;
		    <c:if test="${hasPreviousPageArea}"><a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=1">1</a>&nbsp;<a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=${pageArea[0]-1}">...</a></c:if><c:forEach var="page" items="${pageArea}"><c:choose><c:when test="${page == nextPageIndex}"><span class="ft3"><strong>${page}</strong></span>&nbsp;</c:when><c:otherwise><a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=${page}">${page}</a>&nbsp;</c:otherwise></c:choose><c:set var="maxAreaPage" value="${page}"/></c:forEach><c:if test="${hasNextPageArea}"><a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=${maxAreaPage+1}">...</a>&nbsp;<a href="javascript:void(0)" id="<c:url value='${lookupPathForRequest}'/>?page=${pageCount}">${pageCount}</a></c:if>
		    &nbsp;&nbsp;&nbsp;
		    <c:if test="${!isLastPage}">
		      <input type="hidden" name="nextPageIndex" value="${nextPageIndex}">
		      <input type="hidden" name="recordStartIndex" value="${nextRecordStartIndex}">
					<a href="javascript:void(0)" id="goNextPage.htm">下一页</a>
		    </c:if>
		    <c:if test="${isLastPage}">下一页</c:if>
				<input type="text" class="txtbox" size="4" name="pageTxt" />&nbsp;
				<a href="javascript:void(0)" id="goInputPage.htm">GO</a>
			</div>
</form>

<script type="text/javascript">
	var lookupPathForRequest = "<c:url value='${lookupPathForRequest}'/>";

	$(document).ready(function() {
    var pageForm = $("form[id=pageForm]");

		pageForm.find("a[id*=goPreviousPage.htm]").click(function(){
	    var pageDirection = "previous";
	    turnToPage(pageDirection);
			return false;
		});

		pageForm.find("a[id*="+lookupPathForRequest+"?page]").click(function(){
			var pageNum = getUrlParamOfHrefId($(this));
	    turnToPage(pageNum);
			return false;
		});

		pageForm.find("a[id*=goInputPage.htm]").click(function(){
    	var pageForm = $("form[id=pageForm]");
    	var pageTxt = pageForm.find("input[name=pageTxt]").val();

    	
    	if (!validatePositiveInteger(pageTxt)) {
    		alert("页码输入无效");
				return false;
    	}
    	
    	var maxPageCount = ${pageCount};
    	var inputPageCount = parseInt(pageTxt);
    	
    	if (inputPageCount > (maxPageCount = parseInt(maxPageCount))) {
    		inputPageCount = maxPageCount;
      }
        
	    turnToPage(inputPageCount);
			return false;
		});

    var pageTxt = pageForm.find("input[name=pageTxt]");
    pageTxt.keypress(function(e) {
    	if (e.which == 13) {//回车按下
    		var page =  $(this).val();
    		
	    	if (!validatePositiveInteger(page)) {
	    		alert("页码输入无效");
					return false;
	    	}
	    	
	    	var maxPageCount = ${pageCount};
	    	var inputPageCount = parseInt(page);
	    	
	    	if (inputPageCount > (maxPageCount = parseInt(maxPageCount))) {
	    		inputPageCount = maxPageCount;
	      }
		      
		    turnToPage(inputPageCount);
    		return false;
    	}
    });

		pageForm.find("a[id*=goNextPage.htm]").click(function(){
	    var pageDirection = "next";
	    turnToPage(pageDirection);
			return false;
		});

		function turnToPage(page) {
	    var pageForm = $("form[id=pageForm]");
	    var pageDirection = pageForm.find(":hidden[name=pageDirection]");

	    if (page == "next" || page == "previous") {
		    pageDirection.val(page);
		  } else {
		    var currentPageIndex = page - 1;
		    var recordStartIndex = currentPageIndex * ${pageSize};//page size(maybe change)
		    var nextPageIndexObjOrArr = pageForm.find(":hidden[name=nextPageIndex]");
		    var recordStartIndexObjOrArr = pageForm.find(":hidden[name=recordStartIndex]");
		    nextPageIndexObjOrArr.val(currentPageIndex);
		    recordStartIndexObjOrArr.val(recordStartIndex);
			  if (nextPageIndexObjOrArr.length > 1){
			    $(nextPageIndexObjOrArr[0]).remove();//防止递交控件数组，使得翻页失败
			    $(recordStartIndexObjOrArr[0]).remove();
				}
		    pageDirection.remove();//递交时去除无用控件
		    pageTxt.remove();
		  }
	    pageForm.submit();
	    
	    //ajax implementaion not yet....
		}
	});
</script>